package com.tarena.lbs.basic.web.controller;

import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.base.protocol.model.Result;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.basic.web.service.IDictItemService;
import com.tarena.lbs.common.basic.converter.DictItemConverter;
import com.tarena.lbs.pojo.basic.bo.DictItemBO;
import com.tarena.lbs.pojo.basic.param.DictItemParam;
import com.tarena.lbs.pojo.basic.query.DictItemQuery;
import com.tarena.lbs.pojo.basic.vo.DictItemVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * 字典数据基本信息控制层
 */
@RestController
@RequestMapping("/admin/basic/dictItem/info")
@Api(tags = "字典数据信息模块")
public class DictItemController {
    @Resource
    private IDictItemService dictItemService;

    @Resource
    private DictItemConverter dictItemConverter;


    @ApiOperation("字典数据基本信息新增")
    @PostMapping("/save")
    public Result save(@RequestBody DictItemParam dictItemParam) throws BusinessException {
        dictItemService.save(dictItemParam);
        return Result.success();
    }

    @ApiOperation("根据id删除")
    @DeleteMapping("/delete/{id}")
    public Result<Void> delete(@PathVariable("id") Integer id) {
        dictItemService.delete(id);
        return Result.success();
    }

    @ApiOperation("字典数据基本信息更新")
    @ApiImplicitParams(
            value = {
                    @ApiImplicitParam(name = "dictItemParam", value = "DictItemParam",
                            required = false, dataType = "DictItemParam")
            })
    @PutMapping("/update/{id}")
    public Result<Void> update(@PathVariable("id") Integer id, @RequestBody DictItemParam dictItemParam) throws BusinessException {
        dictItemService.update(id, dictItemParam);
        return Result.success();
    }

    @ApiOperation("字典数据基本信息详情")
    @GetMapping("/detail/{id}")
    public Result<DictItemVO> detail(@PathVariable("id") Integer id) {
        DictItemBO detail = dictItemService.detail(id);
        return new Result<>(dictItemConverter.bo2vo(detail));
    }


    @ApiOperation("字典数据基本信息分页查询")
    @GetMapping("/list")
    public Result<PageResult<DictItemVO>> list(DictItemQuery dictItemQuery) {
        PageResult<DictItemVO> dictItemVOPaginationResult = dictItemService.list(dictItemQuery);
        return new Result<>(dictItemVOPaginationResult);
    }

    @ApiOperation("字典数据基本信息getlistAll查询")
    @GetMapping("/getlistAll/{dictId}/")
    public Result<List<DictItemVO>> listByDictId(@PathVariable("dictId") Integer dictId) {
        List<DictItemBO> list = dictItemService.getlistAll(dictId);
        return new Result<>(dictItemConverter.boListvoList(list));
    }


}
